package org.example;

import java.util.Scanner;

/**
 * Shoppingtest
 * @author 28189
 * @since 2024/11/30 14:48
*/public class Shoppingtest {
    public static void main(String[] args) {
        Shopping[] goods = new Shopping[10];
        goods[0] = new Shopping("可乐", 3, "饮料");
        goods[1] = new Shopping("薯片", 5, "零食");
        goods[2] = new Shopping("方便面", 4, "零食");
        goods[3] = new Shopping("雪碧", 3, "饮料");
        goods[4] = new Shopping("苹果", 5, "水果");
        goods[5] = new Shopping("香蕉", 3, "水果");
        goods[6] = new Shopping("自热米饭", 8, "零食");
        goods[7] = new Shopping("酸奶", 4, "饮料");
        goods[8] = new Shopping("辣条", 2, "零食");
        goods[9] = new Shopping("橙子", 4, "水果");
        System.out.println("可选择商品");
        System.out.println("商品名称\t单价\t商品类型");
        for (int i = 0; i < goods.length; i++) {
            if (goods[i] != null) {
                System.out.println(goods[i].getName() + "\t" + goods[i].getPrice() + "\t" + goods[i].getBrand());
            }
        }
        Shopping[] choice = new Shopping[10];
        int[] num = new int[10];
        int j = 0;
        int i;
        while(j<10){
            int flag=0;
            System.out.println("请输入商品名称：");
            Scanner sc = new Scanner(System.in);
            String name = sc.nextLine();
            for (i = 0; i < goods.length; i++) {
                if (goods[i].getName().equals(name)) {
                    flag=1;
                    choice[j] = goods[i];
                    break;
                }
                else{
                    flag=0;
                }
            }
            if(flag==0){
                System.out.println("没有该商品");
            }
            if(flag==1){
                System.out.println("购买成功,选择购买数量");
                num[j] = sc.nextInt();
                sc.nextLine();
                j++;
            }
            System.out.println("是否继续购买(over=结束,continue=继续,delete=删除商品)");
            String over = sc.nextLine();
            if (over.equals("over")) {
                break;
            }
            if (over.equals("delete")) {
                System.out .println("请输入要删除的商品名称：");
                String delete = sc.nextLine();
                for (i = 0; i < j; i++) {
                    if (choice[i].getName().equals(delete)) {
                        for (int k = i; k < j - 1; k++) {
                            choice[k] = choice[k + 1];
                            num[k] = num[k + 1];
                        }
                    }
                }
                System.out.println("删除成功");
                j--;
            }
        }
        double sum = 0;
        for (i = 0; i < j; i++) {
            sum += choice[i].getPrice() * num[i];
        }
        System.out.println("购买的商品为：");
        for (i = 0; i < j; i++) {
            System.out.println(choice[i].getName());
            System.out.println("数量为:"+num[i]);
        }
        System.out.println("总价为：" + sum);
    }
} 
